package com.byxy.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.hql.internal.ast.HqlASTFactory;
import org.springframework.stereotype.Repository;

import com.byxy.dao.UserDao;
import com.byxy.entity.User;
import com.sun.accessibility.internal.resources.accessibility;

@Repository
public class UserDaoImpl implements UserDao {
@Resource
private SessionFactory sessionFactory;

	@Override
	public void add(User s) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().save(s);

	}

	@Override
	public void delete(User s) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().delete(s);

	}

	@Override
	public void update(User s) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().update(s);

	}

	@Override
	public List<User> find() {
		String hql=" from "+User.class.getSimpleName();
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}

	@Override
	public List<User> find(int pos, int rows) {
		String hql=" from "+User.class.getSimpleName();
		return sessionFactory.getCurrentSession().createQuery(hql).setFirstResult(pos).setMaxResults(rows).list();
	}

	@Override
	public User getById(Serializable id) {
		// TODO Auto-generated method stub
		return (User) sessionFactory.getCurrentSession().get(User.class, id);
	}

	@Override
	public long count() {
		String hql="select count(u) from "+User.class.getSimpleName()+" u";
		return (long) sessionFactory.getCurrentSession().createQuery(hql).uniqueResult();
	}




}
